/*导入需要的模块包*/
var request = require('request');
var http = require('http');
var fs = require('fs');//文件操作
var express = require('express');
var bodyParser = require('body-parser');

//console.log(AipFace);
//实例化
var app = express();
var AipFaceClient = require("baidu-aip-sdk").face;
console.log(AipFaceClient);
// 设置APPID/AK/SK
var APP_ID = "11521565";
var API_KEY = "RGxRXFNiaoDMaFMweuIlWkWI";
var SECRET_KEY = "QmtFrt6WLyVodW78jakO9UfL35DKiBQv";

// 新建一个对象，建议只保存一个对象调用服务接口
var client = new AipFaceClient(APP_ID, API_KEY, SECRET_KEY);

//将图片解析为二进制流
//读取照片
var image = fs.readFileSync('images/hedy.png');
//console.log(image);
//转化为base64格式数据
var base64Img = new Buffer(image).toString('base64');
var imageType = "BASE64";

app.use(bodyParser.urlencoded({limit:'50mb',extended:true}));
//跨域问题解决
app.all('*',function (req,res,next) {
    res.header('Access-Control-Allow-Origin','*');
    next();
})
app.post('/',function(req,res){
    //客户端传输过来的数据
    var imgData = req.body.imUrl;
    console.log(imgData);
    //数据格式处理，正则表达式
    var base64Data = imgData.replace(/\s/g,"+");//去掉空格
    base64Data = base64Data.replace(/^data:image\/\w+;base64,/,"");
    //人脸对比接口
    client.match([base64Img1,base64Img2]).then(function(result){
        var score = result.result[0].score;
        //console.log(JSON.stringify(result));
        res.json(score); //向客户端发送数据 对比分数
    });
    res.json(req.body);
}).listen(80);
//console.log(base64Img);
// 调用人脸检测
client.detect(base64Img,imageType,{face_field:'age'}).then(function(result) {
    console.log(JSON.stringify(result));
});



